﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Web;
using System.Xml;

namespace EPiServer.Municipality.Web
{
    public class SyndicationResult<T> where  T : SyndicationFeedFormatter
    {
        public SyndicationFeed Feed { get; set; }

        public void ExecuteResult()
        {
            SyndicationFeedFormatter formatter = null;

            switch (typeof(T).Name)
            {
                case "Rss20FeedFormatter":
                    HttpContext.Current.Response.ContentType = "application/rss+xml";
                    formatter = Feed.GetRss20Formatter();
                    break;

                case "Atom10FeedFormatter":
                    HttpContext.Current.Response.ContentType = "application/atom+xml";
                    formatter = Feed.GetAtom10Formatter();
                    break;

                default:
                    throw new ApplicationException();
            }
            using (XmlWriter writer = XmlWriter.Create(HttpContext.Current.Response.Output))
            {
                if (writer != null) formatter.WriteTo(writer);
            }
        }
         
    }
}
